package com.xbao.service.impl;

import com.xbao.core.util.MyBeanUtil;
import com.xbao.core.util.pub.exception.DBExceptionEnums;
import com.xbao.core.util.pub.exception.ServiceException;
import com.xbao.dao.StuContractMapper;
import com.xbao.dao.StuMapper;
import com.xbao.model.StuContract;
import com.xbao.model.StuContractExample;
import com.xbao.model.req.StuAccountReq;
import com.xbao.model.req.StuContractReq;
import com.xbao.model.vo.FeeTotalVo;
import com.xbao.model.vo.StuContractVo;
import com.xbao.model.vo.StuVo;
import com.xbao.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by qingshengzheng on 18/8/30.
 */
@Service
public class StuContractServiceImpl implements StuContractService {
    @Resource
    private StuContractMapper contractMapper;
    @Resource
    private StuAccountService accountService;
    @Resource
    private StuService stuService;

    @Resource

    private PeriodRecordService recordService;
    @Resource
    private PeriodRuleService ruleService;




    /**
     *
     * @param feeReq
     */
    @Override
    public void addContract(StuContractReq feeReq) {
        if(feeReq.getSid()==null) {
            stuService.addStu(feeReq.getStuReq());
        }else {
            throw new ServiceException(DBExceptionEnums.USER_NO_EXISTS);
        }
        recharge(feeReq);
    }

    /**
     *
     * @param id
     */
    @Override
    public void deleteContract(Long id) {
        contractMapper.deleteByPrimaryKey(id);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public StuContractVo detailContract(Long id) {
        StuContract contract = contractMapper.selectByPrimaryKey(id);
        StuContractVo vo = null;
        if (contract != null) {
            vo = MyBeanUtil.copyProperties(contract, StuContractVo.class);
            StuVo stuDetail=stuService.detail(id);
            vo.setStuVo(stuDetail);

        }
        return vo;

    }

    /**
     * @param req
     * @return
     */
    @Override
    public List<StuContractVo> queryContractPage(StuContractReq req) {
        StuContractExample example = new StuContractExample();
        StuContractExample.Criteria criteria = example.createCriteria();
        if (req.getSid() != null) {
            criteria.andSidEqualTo(req.getSid());
        }
        List<StuContract> fees = contractMapper.selectByExample(example);
        List<StuContractVo> vos = MyBeanUtil.copyList(fees, StuContractVo.class);
        for(StuContractVo vo:vos){
            wrapStuVo(vo);
        }
        return vos;
    }
    public void wrapStuVo(StuContractVo contractVo){
        if(contractVo!=null&&contractVo.getSid()!=null){
            StuVo stuVo=stuService.detail(contractVo.getSid());
            contractVo.setStuVo(stuVo);
        }
    }




    /**
     * @param feeReq
     */
    @Override
    public void recharge(StuContractReq feeReq) {
        contractMapper.insert(feeReq);
        StuAccountReq req = feeReq.toAccountReq();
        accountService.charge(req);
        if(feeReq.getRecord()!=null){
            recordService.addPeriodRecord(feeReq.getRecord());

        }
        if(feeReq.getRule()!=null){
            ruleService.addPeriodRule(feeReq.getRule());

        }





    }

    /**
     * @param beginTime
     * @param endtime
     * @param orgid
     * @return
     */
    @Override
    public Integer needCharge(Long beginTime, Long endtime, Long orgid) {
        StuContractExample example = new StuContractExample();
      /*  example.
        StuContractExample.Criteria criteria = example.createCriteria();
        criteria.andChargetimeBetween(beginTime, endtime);
*/
        Integer n=contractMapper.countByExample(example);
        return n;
    }

    /**
     * @param beginTime
     * @param endtime
     * @param orgid
     * @return
     */
    @Override
    public Integer realCharge(Long beginTime, Long endtime, Long orgid) {
        return 0;
    }

    /**
     * @param beginTime
     * @param endtime
     * @param orgid
     * @return
     */
    @Override
    public Integer signCharge(Long beginTime, Long endtime, Long orgid) {
        return 0;
    }

    /**
     *
     * @param beginTime
     * @param endtime
     * @param orgid
     * @return
     */
    @Override
    public Integer recharge(Long beginTime, Long endtime, Long orgid) {
        return 0;
    }

    @Override
    public Integer chargeback(Long beginTime, Long endtime, Long orgid) {
        return null;
    }

    /**
     * 时间范围内的费用统计
     *
     * @param beginTime
     * @param endtime
     * @param orgid
     * @return
     */
    @Override
    public FeeTotalVo queryFeeTotalVo(Long beginTime, Long endtime, Long orgid) {
        //应收费用
        Integer n = needCharge(beginTime, endtime, orgid);
        //实手费用
        Integer r = realCharge(beginTime, endtime, orgid);
        //新签费用
        Integer s = signCharge(beginTime, endtime, orgid);
        //徐费费用
        Integer re = recharge(beginTime, endtime, orgid);
        FeeTotalVo vo = new FeeTotalVo();
        vo.setNeedCharge(n);
        vo.setRealcharge(r);
        vo.setSignCharge(s);
        vo.setRecharge(re);
        return vo;
    }
}
